package com.breaktian.demoapp.ui.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.alibaba.fastjson.TypeReference;
import com.breaktian.assemble.router.VpRouter;
import com.breaktian.assemble.utils.AppInfoUtil;
import com.breaktian.assemble.utils.LogUtil;
import com.breaktian.demoapp.R;
import com.breaktian.demoapp.hotfix.andfix.AndFixManager;
import com.breaktian.demoapp.provider.TestProvider;
import com.breaktian.demoapp.utils.ToastUtils;
import com.breaktian.network.api.finance.ApiCallback;
import com.breaktian.network.api.finance.ApiResult;
import com.breaktian.network.api.finance.BaseFinaceApi;
import com.breaktian.network.http.Http;
import com.breaktian.share.Share;
import com.google.gson.Gson;

/**
 * A simple {@link Fragment} subclass.
 * 首页
 */
public class IndexFragment extends BaseFragment<TestProvider> {


    private View rootView;
    private Button btn_cordova;
    private Button btn_test_basemodule_activity;
    private Button btn_get;
    private Button btn_post;
    private Button btn_andfix;
    private Button btn_tinker;
    private Button btn_share;
    private Button btn_usercenter;
    private Button btn_login;
    private Button btn_tabviewpager;

    public IndexFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_index, container, false);
        initView();
        return rootView;
    }

    private void initView() {
        btn_cordova = (Button)rootView.findViewById(R.id.btn_cordova);
        btn_test_basemodule_activity = (Button)rootView.findViewById(R.id.btn_test_basemodule_activity);
        btn_cordova.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                VpRouter.get().activity(getActivity()).jump("http://www.hbounty.com/test/cordova/sample.html");
            }
        });
        btn_test_basemodule_activity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                VpRouter.get().activity(getActivity()).jump("demo://base.testactivity");
            }
        });
        btn_get = (Button)rootView.findViewById(R.id.btn_get);
        btn_get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new TestApi().get("http://hbounty.com/test/api/get.php");
            }
        });
        btn_post = (Button)rootView.findViewById(R.id.btn_post);
        btn_post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new TestApi().post("http://hbounty.com/test/api/post.php");
            }
        });
        btn_andfix = (Button)rootView.findViewById(R.id.btn_andfix);
        btn_andfix.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AndFixManager.getInstance().loadPatchFromServer(String.format("http://www.hbounty.com/test/andfix/v_%s/dest.apatch", AppInfoUtil.getVersionName(IndexFragment.this.getContext())));
            }
        });

//        btn_andfix.setText("我是热修复之后的button");
//        btn_andfix.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                ToastUtil.show("我是打补丁之后的版本");
//            }
//        });
        btn_tinker = (Button)rootView.findViewById(R.id.btn_tinker);
//        btn_tinker.setText("我是tinker热修复之后的button");
        btn_tinker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToastUtils.show("我是打过tinker补丁之后的版本");
            }
        });
        btn_share = (Button)rootView.findViewById(R.id.btn_share);
        btn_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                share();
            }
        });
        btn_usercenter = (Button)rootView.findViewById(R.id.btn_usercenter);
        btn_usercenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                VpRouter.get().activity(getActivity()).jump("demo://userCenter");
            }
        });
        btn_login = (Button)rootView.findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                VpRouter.get().activity(getActivity()).jump("demo://login");
            }
        });
        btn_tabviewpager = (Button)rootView.findViewById(R.id.btn_tabviewpager);
        btn_tabviewpager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                VpRouter.get().activity(getActivity()).jump("demo://tabviewpager");
            }
        });

        rootView.findViewById(R.id.btn_jetpack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                VpRouter.get().activity(getActivity()).jump("demo://jetpack");
            }
        });



    }

    private void share() {
//        Share.get(this.getActivity()).setChannels("1,2,3,4,5").shareImage("https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2603404175,2805914899&fm=173&app=25&f=JPEG?w=640&h=960&s=F001DF1C4A77768030A66041030060E9");
        Share.get(this.getActivity()).setChannels("1,2,3,4,5").share("111","内容","https://www.baidu.com/img/bd_logo1.png?where=super","https://www.baidu.com");
    }

    @Override
    protected TestProvider getProvider() {
        return null;
    }

    public static class TestApi extends BaseFinaceApi{
        public void get(String url){
            Http http = Http.get(url);
            ApiResult<TestEntity> cache = request(http, new TypeReference<ApiResult<TestEntity>>() {
            }, new ApiCallback<TestEntity>() {
                @Override
                public void onResult(ApiResult<TestEntity> apiResult) {
                    LogUtil.i("http onResult apiResult:"+ new Gson().toJson(apiResult));
                    ToastUtils.show(new Gson().toJson(apiResult));
                }

                @Override
                public void onSuccess(TestEntity data) {
                    LogUtil.i("http onSuccess data:"+ new Gson().toJson(data));
                }

                @Override
                public void onFail(ApiResult.ApiError apiError) {
                    LogUtil.i("http onFail apiError:"+ new Gson().toJson(apiError));

                }
            });
            LogUtil.i("http cache:"+ new Gson().toJson(cache));
            ToastUtils.show(new Gson().toJson(cache));
        }
        public void post(String url){
            Http http = Http.post(url).param("title","我的title").param("desc","i am desc");
            ApiResult<TestEntity> cache = request(http, new TypeReference<ApiResult<TestEntity>>() {
            }, new ApiCallback<TestEntity>() {
                @Override
                public void onResult(ApiResult<TestEntity> apiResult) {
                    LogUtil.i("http onResult apiResult:"+ new Gson().toJson(apiResult));
                    ToastUtils.show(new Gson().toJson(apiResult));
                }

                @Override
                public void onSuccess(TestEntity data) {
                    LogUtil.i("http onSuccess data:"+ new Gson().toJson(data));
                }

                @Override
                public void onFail(ApiResult.ApiError apiError) {
                    LogUtil.i("http onFail apiError:"+ new Gson().toJson(apiError));

                }
            });
            LogUtil.i("http cache:"+ new Gson().toJson(cache));
            ToastUtils.show(new Gson().toJson(cache));
        }

    }

    public static class TestEntity{
        public String title;
        public String desc;
    }

}
